clear all
clear matrix
version 13
capture log close
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------
	global main		= "\\iab.baintern.de\DFS\017\Ablagen\D01700-Data\N2066017\Datenaustausch\GradAB\Schnitzlein_Stueber\Stueber\CADAL_Flow_Dataset\Federica"
	global data_orig= "N:\Ablagen\D01700-IAB-Projekte\D01700-CADAL_Flow_Dataset\package"
	global data_proc= "$main"+"\"+"temp"
	global log   	= "$main"+"\"+"log"
	global do		= "$main"+"\"+"do"
	global data_add = "$main"+"\"+"data"
*-------------------------------------------------------------------------------
cd "$main"
*-------------------------------------------------------------------------------
********************************************************************************
* INPUT: RAW DATA
* OUTPUT: GET QUARTERLY LOCAL INDUSTRY-LEVEL EMPLOYMENT TIME SERIES
********************************************************************************
use "$data_proc\data_federica_1990_2014_west.dta", clear 

gen quarter = q - 4 * (a - 1)
label var a "index of year"
label var year "year"
label var st_reg "stock regular workers"
label var founded_q "Quarter in which establishment founded"
label var quarter "Quarter (1-4)"
label var district "district (Kreis) of establishment"

rename st_reg employment

* Count of (new) establishments
gen count_all = 1
gen count_new = 0
replace count_new = 1 if q == founded_q

* Create time variable:
gen eventdate=yq(year,quarter)
format eventdate %tq
drop year quarter

* Generate geographical identifier for each district (5-digits code) and save it:
egen id_geo=group(district) 
label var id_geo "district identifier"

preserve
  keep id_geo district
  duplicates drop
  qui: saveold "$data_proc/id_district.dta", replace version(14)
restore

rename w93_imp w93_3
tab w93_3
********************************************************************************
* Aggregate:
egen id=group(id_geo w93_3)
collapse (sum) employment (mean) w93_3 id_geo, by(eventdate id)
qui: saveold "$data_proc/beforeseasonaladjustment.dta", replace version(14)
********************************************************************************
